export default {
	data() {
		return {
			userInfo: this.$store.state.login || {},
			zheCount: 0,
			manCount: 0,
			creditsTotal: 0,
			dotNum: 0,
			managerData: {}
		}
	},
	async onLoad() {
		//await this.getPageData();
	},
	async onShow() {
		//await this.getNum();
	},
	async onPullDownRefresh() {
		//await this.getPageData();
		uni.stopPullDownRefresh();
	},
	onShareAppMessage() {
		return {
			path: `/pages/home/index/index`,
			title: '硒乡坊',
			success() {
				console.log('onShareAppMessage: 分享面板调起成功，不代表分享成功');
			},
			fail(e) {
				console.log('onShareAppMessage: 分享面板调起失败', e);
			}
		}
	},
	methods: {
		async getPageData() {
			await Promise.all([
				this.getUserInfo(),
				this.getCouponCount(),
				this.getCreditsTotal(),
				this.getManagerData(),
				this.getNum()
			]);
		},
		async getManagerData() {
			if (this.userInfo.role === 9) {
				const getRes = await this.$uniCloudCallFunction({
					name: "xxf-manager",
					action: "getMineData",
					data: {
						uid: this.$store.state.login.uid,
					}
				});
				console.log("getRes: ", getRes);
				this.managerData = getRes;
			}
		},
		async getNum() {
			const numRes = await this.$uniCloudCallFunction({
				name: 'xxf-order',
				action: 'getTabbarNum',
				data: {
					uid: this.$store.state.login.uid,
					role: this.$store.state.login.role
				}
			});
			console.log("numRes: ", numRes);
			this.dotNum = numRes || 0;
		},
		async getCreditsTotal() {
			const getRes = await this.$uniCloudCallFunction({
				name: "xxf-credits",
				action: "getCreditsTotal",
				data: {
					uid: this.$store.state.login.uid,
				}
			});
			console.log("getRes: ", getRes);
			if (getRes) {
				this.creditsTotal = getRes.creditsTotal;
			}
		},
		async getCouponCount() {
			const getRes = await this.$uniCloudCallFunction({
				name: "xxf-coupon",
				action: "getCouponCount",
				data: {
					uid: this.$store.state.login.uid,
				}
			});
			console.log("getRes: ", getRes);
			if (getRes) {
				this.zheCount = getRes.zheCount;
				this.manCount = getRes.manCount;
				if (this.userInfo.role === 2 || this.userInfo.role === 9) this.zheCount = this.zheCount + 1;
			}
		},
		async getUserInfo() {
			const getRes = await this.$uniCloudCallFunction({
				name: "xxf-user",
				action: "getUser",
				data: {
					uid: this.$store.state.login.uid,
				}
			});
			console.log("getRes: ", getRes);
			if (getRes && getRes._id) {
				this.$store.commit("login/setLoginInfo", {
					openId: getRes.open_id,
					uid: getRes._id,
					token: getRes.token,
					role: getRes.role,
					mobile: getRes.mobile,
					nickname: getRes.nickname,
					avatarUrl: getRes.avatar_url,
					createTime: getRes.create_time,
				});
				this.userInfo = this.$store.state.login || {};
			}
		},
		toProfile() {
			uni.navigateTo({
				url: "/pages/minePack/profile/index"
			});
		},
		toMessages() {
			uni.navigateTo({
				url: "/pages/minePack/messages/index"
			});
		},
		toCouponList(type) {
			uni.navigateTo({
				url: `/pages/minePack/couponList/index?type=${type}`
			});
		},
		toCreditList() {
			uni.navigateTo({
				url: "/pages/minePack/creditList/index"
			});
		},
		toOrders(type) {
			uni.navigateTo({
				url: `/pages/minePack/orders/index?type=${type}`
			});
		},
		toVip() {
			uni.navigateTo({
				url: `/pages/minePack/vip/index`
			});
		},
		toCreditShop() {
			uni.navigateTo({
				url: `/pages/minePack/creditShop/index`
			});
		},
		toExplain() {
			uni.navigateTo({
				url: `/pages/minePack/explain/index`
			});
		},
		toFeedback() {
			uni.navigateTo({
				url: `/pages/minePack/feedback/index`
			});
		},
		handleContact(e) {
			// 小程序消息页面的路径 String
			console.log(e.detail.path)
			// 小程序消息指定的查询参数 Object
			console.log(e.detail.query)
		},
		toAbout() {
			uni.navigateTo({
				url: `/pages/minePack/about/index`
			});
		},
		toSetting() {
			uni.navigateTo({
				url: `/pages/minePack/setting/index`
			});
		},
		toManager() {
			uni.navigateTo({
				url: `/pages/managerPack/manager/index`
			});
		},
		toShipping() {
			uni.navigateTo({
				url: "/pages/managerPack/shipping/index"
			})
		}
	}
}